﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Super.Common.Tools
{
    class RMB
    {
        /// <summary>
        /// 人民币大小写转换。
        /// </summary>
        /// <param name="r">货币金额</param>
        /// <returns>人民币大写字符串</returns>
        /// <remarks>
        /// 超过两位的小数会自动进行四舍五入。
        /// </remarks>
        public static string DecimalToChinese(decimal value)
        {
            try
            {
                if (value == 0) return "零元";

                string[] cnShuzi = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
                string[] cnDanwei = {  "分", "角", "元", "拾", "佰", "仟", "万", "拾万", "佰万", "仟万", 
                    "亿", "拾亿", "佰亿", "仟亿", "万亿", "拾万亿", "佰万亿", "仟万亿", "万万亿" };

                char[] arr = decimal.ToInt64(decimal.Round(value, 2) * 100).ToString().ToCharArray();
                Array.Reverse(arr);

                StringBuilder rmb = new StringBuilder();

                for (int i = 0; i < arr.Length; i++)
                {
                    int num = int.Parse(arr[i].ToString());

                    if (num != 0)
                    {
                        if (i > 6 && i < 10 && rmb.ToString().IndexOf("万") >= 0)
                            rmb.Insert(0, cnShuzi[num] + cnDanwei[i][0]);
                        else if (i > 10 && i < 14 && rmb.ToString().IndexOf("亿") >= 0)
                            rmb.Insert(0, cnShuzi[num] + cnDanwei[i][0]);
                        else if (i > 14 && rmb.ToString().IndexOf("万亿") >= 0)
                            rmb.Insert(0, cnShuzi[num] + cnDanwei[i].Substring(0, 2));
                        else
                            rmb.Insert(0, cnShuzi[num] + cnDanwei[i]);
                    }
                    else
                    {
                        if (i == 2)
                        {
                            if (arr[1] == '0')
                                rmb.Insert(0, "元");
                            else
                                rmb.Insert(0, "元零");
                        }

                        if (rmb.Length > 0)
                        {
                            if (rmb[0] != '零' && rmb[0] != '元')
                                rmb.Insert(0, "零");
                        }
                    }
                }

                return rmb[rmb.Length - 1] == '元' ? rmb.ToString() + "整" : rmb.ToString();
            }
            catch
            {
                return "数值过大，无法转换！";
            }
        }

        /**/
        /// <summary> 
        /// 一个重载，将字符串先转换成数字在调用CmycurD(decimal num) 
        /// </summary> 
        /// <param name="num">用户输入的金额，字符串形式未转成decimal</param> 
        /// <returns></returns> 
        public static string CmycurD(string numstr)
        {
            try
            {
                decimal num = Convert.ToDecimal(numstr);
                return DecimalToChinese(num);
            }
            catch
            {
                return "非数字形式！";
            }
        }
    }
}
